package com.common.test;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.*;
import java.net.SocketException;

public class WindowsShell {
    TelnetClient telnet = new TelnetClient("VT220");

    InputStream in;
    PrintStream out;

    String prompt = ">";

    public WindowsShell(String ip, int port, String user, String password) {
        try {
            telnet.connect(ip, port);
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());
            login(user, password);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 登录
     *
     * @param user
     * @param password
     */
    public void login(String user, String password) {
        readUntil("\rlogin: ");
        write(user);
        readUntil("\rpassword: ");
        write(password);
        readUntil(prompt + "");
    }

    /**
     * 读取分析结果
     *
     * @param pattern
     * @return
     */
    public String readUntil(String pattern) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int bufferSize = 10240;
            final byte[] buf = new byte[bufferSize]; // BUFSIZE 是一个常量10240=10k
            int read = 0;
            while(read != -1){
                read = in.read(buf);
                outputStream.write(buf, 0, read);
                String str = outputStream.toString("gb2312");
                if(str.endsWith(pattern)){
                    return str;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    /**
     * 写操作
     *
     * @param value
     */
    public void write(String value) {
        try {
            out.println(value);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 向目标发送命令字符串
     *
     * @param command
     * @return
     */
    public String sendCommand(String command) {
        try {
            write(command);
            return readUntil(prompt + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭连接
     */
    public void disconnect() {
        try {
            telnet.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        WindowsShell ws = new WindowsShell("10.16.6.52", 23, "admin", "admin");
//			System.out.println(ws);
        // 执行的命令
        String str = ws.sendCommand("cmd /c taskkill /f /im chrome.exe");
        System.out.println(str);
        ws.disconnect();
    }

}
